<template>
  <div class=".list-pagination employeeManagement-container">
    <div class="employeeManagement-container-header">
      <el-input v-model="employeeName" class="input" placeholder="请输入员工姓名" @keydown.enter.native="searchEmployee">
        <el-button slot="append" icon="el-icon-search" style="padding:0" @click="searchEmployee" />
      </el-input>
      <el-button type="primary" icon="el-icon-plus" @click="addEmployee">添加员工</el-button>
    </div>
    <el-table
      :data="employeeInformation"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="name"
        label="员工姓名"
        min-width="120px"
      />
      <el-table-column
        prop="username"
        label="员工账号"
        min-width="200px"
        align="center"
      />
      <el-table-column
        prop="phone"
        label="手机号"
        min-width="150px"
        align="center"
      />
      <el-table-column
        prop="status"
        label="状态"
        min-width="120px"
        :formatter="formatterStatus"
        align="center"
      />
      <el-table-column label="操作" width="150px" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            :disabled="judgeStatus(scope.row)"
            @click="handleForbidden(scope.$index, scope.row)"
          >禁用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="paginationInfo.total > 0"
      :total="paginationInfo.total"
      :page.sync="paginationInfo.page"
      :limit="paginationInfo.pageSize"
      @pagination="getTableData"
    />
    <employee-dialog v-if="showEmployeeDialog" :show.sync="showEmployeeDialog" :is-edit="isEdit" :employee-id="employeeId" />
  </div>
</template>

<script>
// import { mapGetters } from 'vuex'
import { getEmployeeList, enableEmployee } from '@/api/employee'
import EmployeeDialog from '@/views/employeeManagement/components/EmployeeDialog'
import { EventBus } from '@/utils/eventBus'
import pagination from '@/components/pagination'

export default {
  name: 'EmployeeManagement',
  components: { EmployeeDialog, pagination },
  data() {
    return {
      employeeName: '',
      employeeInformation: [],
      detailEmployee: '',
      showEmployeeDialog: false,
      isEdit: false,
      paginationInfo: {
        pageSize: 10,
        page: 1,
        total: 0
      }
    }
  },
  created() {
    this.getTableData()
  },
  mounted() {
    EventBus.$on('refetchEmployeeData', () => {
      this.paginationInfo.page = 1
      this.getTableData()
    })
  },
  methods: {
    searchEmployee() {
      this.paginationInfo.page = 1
      this.getTableData()
    },
    async getTableData(obj) {
      if (obj) {
        const { page, limit } = obj
        this.paginationInfo.page = page
        this.paginationInfo.pageSize = limit
      }
      const { page, pageSize } = this.paginationInfo
      const { data } = await getEmployeeList({ page, pageSize, name: this.employeeName })
      if (data) {
        const { records, total } = data
        this.employeeInformation = records
        this.paginationInfo.total = total
      }
    },
    handleEdit(index, row) {
      console.log(row.id)
      this.employeeId = row.id
      console.log(row)
      this.isEdit = true
      this.showEmployeeDialog = true
    },
    async handleForbidden(index, row) {
      const params = row
      console.log(row.status)
      params.status = 0
      const result = await enableEmployee(params)
      console.log(result)
      if (result.code) {
        this.paginationInfo.page = 1
        this.getTableData()
      }
    },
    formatterStatus(row) {
      const statusIm = ['已禁用', '使用中']
      return statusIm[row?.status]
    },
    judgeStatus(row) {
      return row?.status === 0
    },
    addEmployee() {
      this.isEdit = false
      this.showEmployeeDialog = true
      this.employeeId = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.employeeManagement {
  &-container {
    margin: 10px;
    &-header{
      display: flex;
      margin: 30px;

      .input{
        width: 250px;
        .el-button{
          margin: 0 !important;
        }
      }
      .el-button{
        margin-left: auto;
      }
    }
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
